﻿/*
 * 2009/08/11
 * For Get Web Information
 * ppkliu
 * 
 * */
package webhandler;

import java.io.*;
//import java.util.*;
//import javax.servlet.*;
import javax.servlet.ServletException;
import javax.servlet.http.*;

import java.net.MalformedURLException;
import java.net.URL;



public class FetchData extends HttpServlet {
	public void doGet(HttpServletRequest requet, HttpServletResponse response)
    throws IOException {
    
	response.setContentType("text/html;charset=utf-8");
	response.setCharacterEncoding("utf-8");
	PrintWriter out = response.getWriter();
	out.println("<html><head><title>Get Stock Number</title></head><body>");
	out.println("<h2>抓取股票資訊!</h2>");
	out.print("<form action=\"FetchData\" method=\"post\">" );
	out.print("請輸入台股股號<p>");
	out.print("<input type=\"text\" name=\"stockno\" size=\"6\"><p>");
	out.print("<input type=submit value=\"送出\">");
	
	out.println("</body></html>");	
	out.close();	

	}
	public void doPost(HttpServletRequest request, HttpServletResponse response)
	throws ServletException, IOException {
	
	String line=null;
	
	//Servlet指定編碼，取得參數
	response.setContentType("text/html;charset=utf-8");
	response.setCharacterEncoding("utf-8");
	PrintWriter out = response.getWriter();
	
	String stockno=request.getParameter("stockno");
//	String stockname = "";
	int k1=0,k2=0;
	int lineno=0;
	int gotline = 0;
	//Yahoo Taiwan Stock information
	String   key ="" ;
	String   Stock_time="";              //顯示股票時間
	float UpDown =0 ;                 //漲跌
	int   dir = 1;   //2: up, 1: equl , 0: down
	int   Snumber=0;                  //張數
	float Price=0;                    //成交價格
	float Buy_Price=0;
	float Sell_Price=0;               //買進，賣出
	float Yes_Price=0;                //昨收
	float Open_Price=0;               //開盤價 
	float HighestP=0;                 //最高價
	float LowestP=0;                  //最低價
    String temp ="" ;
	
	out.println("<html><head><title>Show Stock Information</title></head><body>");
	out.println("請檢視"+stockno+"的相關分析<br>");	
	
	  //Parsing the Web information		
    try {
    	  URL urltw = new URL("http://tw.stock.yahoo.com/q/q?s="+ stockno );
   	      BufferedReader reader = new BufferedReader(new InputStreamReader(urltw.openStream(),"Big5"));     

	
	      while((line=reader.readLine())!=null ){ 
	          lineno=lineno+1;
	                                            
	  	    //Print Now time
	          if(lineno == 157){
//	       	   out.println("Now is "+line.substring(73,90) +"<br>");  //Get Now time   	        
	          }
	       //Print the Stock Price Information	   
	          if(lineno >= 202 && lineno <= 235 ){
	       	   k1=line.indexOf(stockno);
	       	   if(k1>-1){  //not got it!
	                gotline = lineno ;
	              }
	  	    	   out.println(line);	    	   
	  	       }    	    
	          
	          if(lineno == 224){  //Print the Time info
	       	   k1 = line.indexOf(">");
	       	   k2 = line.lastIndexOf("<");
	       	   Stock_time = line.substring(k1+1,k2);  
	          }
	          if(lineno == 225){ //Print the Price info        	   
	       	   k1 = line.indexOf("b>");
	       	   k2 = line.lastIndexOf("</b");        	   
	       	   Price = Float.parseFloat(line.substring(k1+2,k2));        	   
	          }
	          if(lineno == 226){ //Print the Buy Price info        	   
	       	   k1 = line.indexOf(">");
	       	   k2 = line.lastIndexOf("<");        	   
	       	   Buy_Price = Float.parseFloat(line.substring(k1+1,k2));        	   
	          }
	          if(lineno == 227){ //Print the Sell Price info        	   
	       	   k1 = line.indexOf(">");
	       	   k2 = line.lastIndexOf("<");        	   
	       	   Sell_Price = Float.parseFloat(line.substring(k1+1,k2));        	   
	          }
	          if(lineno == 228){ //Print the UpDown info        	   
	       	   k1 = line.lastIndexOf(">");
	       	   k2 = line.length();//lastIndexOf(" ");        	          	    
	       	   UpDown =Float.parseFloat(line.substring(k1+2,k2));  	 
	       	   key = line.substring(k1+1,k1+2);
	       	   
	       	   if(key.equals("△")){      dir = 2; }   //up
	       	   else if(key.equals("▽")){ dir = 0;}
	       	   else {dir = 1;} // equal
	       	   	       	  
	       	   if(dir == 0){UpDown = 0 - UpDown ;}
	       	   
	          }
	           if(lineno == 229){ //Print the Stock number info        	   
		       	   k1 = line.indexOf(">");
		       	   k2 = line.lastIndexOf("<");   
		       	   temp = line.substring(k1+1,k2);
		       	   temp = temp.replaceAll(",", "");		      
		       	   Snumber =Integer.parseInt(temp);  	       	     	       	   
		      } 
	          if(lineno == 230){ //Print the Sell Price info        	   
		       	   k1 = line.indexOf(">");
		       	   k2 = line.lastIndexOf("<");        	   
		       	   Yes_Price = Float.parseFloat(line.substring(k1+1,k2));        	   
	}     
	          if(lineno == 231){ //Print the Open Price info        	   
		       	   k1 = line.indexOf(">");
		       	   k2 = line.lastIndexOf("<");        	   
		       	   Open_Price = Float.parseFloat(line.substring(k1+1,k2));        	   
		      }
	          if(lineno == 232){ //Print the Highest Price info        	   
		       	   k1 = line.indexOf(">");
		       	   k2 = line.lastIndexOf("<");        	   
		       	   HighestP = Float.parseFloat(line.substring(k1+1,k2));        	   
		      }
	          if(lineno == 233){ //Print the Lowest Price info        	   
		       	   k1 = line.indexOf(">");
		       	   k2 = line.lastIndexOf("<");        	   
		       	   LowestP = Float.parseFloat(line.substring(k1+1,k2));        	   
		      } 
	          
	          
	      	  	      	  	      	      
	      }  //end While    
	      
	      reader.close();
   	  	  
	      } catch (MalformedURLException e) {
	    	  out.println("You Must enter correct URL<br>");
	      } catch (IOException e) {
	          out.println("Error:"+e.getMessage()+"<p>");
	          e.printStackTrace();	    	  
}        

          //print line number and some information
  	      out.println("<HR>");
  	      out.println("gotline: " +(gotline)+ "<br>");
  	      out.println("k1: " +(k1)+ "<br>");
  	      out.println("k2: " +(k2)+ "<br>");
  	      
  	      out.println("Time is "+Stock_time +"<br>");
  	      out.println("Price is "+ (Price) +"<br>");	
  	      out.println("Buy_Price is "+ (Buy_Price) +"<br>");	
  	      out.println("Sell_Price is "+ (Sell_Price) +"<br>");
  	      out.println("UpDown is "+ (UpDown) +"<br>");
  	      out.println("dir is "+ (dir) +"<br>"); 
  	      //out.println("UpDown is "+ (dir) +"<br>");
  	      out.println("Snumber is "+ (Snumber) +"<br>"); 
  	      out.println("Yes_Price is "+ (Yes_Price) +"<br>");	
  	      out.println("Open_Price is "+ (Open_Price) +"<br>");	
  	      out.println("Highest Price is "+ (HighestP) +"<br>");
  	      out.println("Lowest Price is "+ (LowestP) +"<br>");

  	 	  out.println("key is "+ (key) +"<br>"); 
  	      out.println("<HR>");



	out.println("</body></html>");	
	out.close();	

	}     
//*/
}        
